﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PADWH35
{
    public partial class BlowJobStatusFunction2 : Form
    {
        public BlowJobStatusFunction2()
        {
            InitializeComponent();
            this.Font = new Font(this.Font.Name, 12);
            datagv.AutoGenerateColumns = false;
            tableLayoutPanel1.Dock = DockStyle.Fill;
            tableLayoutPanel2.Dock = DockStyle.Fill;
            datagv.Dock = DockStyle.Fill;
        }

        private void button1_Click(object sender, EventArgs e)//查询
        {
            BindData();
        }

        private void button4_Click(object sender, EventArgs e)//保存
        {
            SaveData();

            int FDSR = datagv.FirstDisplayedScrollingRowIndex;
            int FDSC = datagv.FirstDisplayedScrollingColumnIndex;
            BindData();
            datagv.FirstDisplayedScrollingRowIndex = FDSR;
            datagv.FirstDisplayedScrollingColumnIndex = FDSC;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.label2.Enabled = (textBox1.Text == "");
            this.sdate.Enabled = (textBox1.Text == "");
            this.edate.Enabled = (textBox1.Text == "");
        }

        private void datagv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            datagv[IsChangedCol.Index, e.RowIndex].Value = true;
            button4.Enabled = true;
        }

        private void datagv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            datagv[IsChangedCol.Index, e.RowIndex].Value = true;
            button4.Enabled = true;
        }

        public void BindData()
        {
            string sql = string.Empty;
            sql += "SELECT JH.JobNum ,JA.PartNum ,CAST(JO.OprSeq AS VARCHAR(9))+' '+JO.OpDesc OpDesc ,JP.PartQty ," + Environment.NewLine +
                   "    CAST(JO.Date01 AS NVARCHAR)+ ' ' +CONVERT(VARCHAR(12), CAST(DATEADD(SS, JO.Number03, 0) AS TIME), 108) AS ActualDate ," + Environment.NewLine +
                   "    JO.CheckBox03 ShowOnKanban ,JO.ShortChar05 AS LIZENZ ,JO.ShortChar06 AS STANDORT ,JH.CheckBox05 Stop ,JA.AssemblySeq ,JO.OprSeq" + Environment.NewLine +
                   "FROM JobHead JH" + Environment.NewLine +
                   "    INNER JOIN ERP.JobAsmbl JA ON JH.Company = JA.Company AND JH.JobNum = JA.JobNum" + Environment.NewLine +
                   "        AND JH.JobReleased = 1" + Environment.NewLine +
                   "        AND LEFT(JH.ShortChar06,6) IN('Normal','Re-wor')" + Environment.NewLine +
                   "        AND JH.Company = 'NSZ'" + Environment.NewLine +
                   "        AND(JH.JobNum = '" + textBox1.Text + "' or '' = '" + textBox1.Text + "')" + Environment.NewLine;
            if (sdate.Enabled) { sql += "        AND JH.CreateDate BETWEEN '" + sdate.Value.ToString("yyyy-MM-dd") + "' AND '" + edate.Value.ToString("yyyy-MM-dd") + "'" + Environment.NewLine; }
            sql += "    INNER JOIN ERP.JobPart JP ON JH.Company = JP.Company AND JH.JobNum = JP.JobNum AND JH.PartNum = JP.PartNum AND JP.JobClosed = 0 AND JP.PartQty > JP.ReceivedQty" + Environment.NewLine +
                   "    LEFT JOIN JobOper JO ON JA.Company = JO.Company AND JA.JobNum = JO.JobNum AND JA.AssemblySeq = JO.AssemblySeq" + Environment.NewLine +
                   "ORDER BY JH.JobNum ,JA.AssemblySeq ,JO.OprSeq";

            //Clipboard.Clear();
            //Clipboard.SetText(sql);

            DataTable dt = DBHelper.ExecuteDt(sql);
            datagv.DataSource = dt;

            datagv.Columns[IsChangedCol.Index].ReadOnly = true;
            datagv.Columns[JobNumCol.Index].ReadOnly = true;
            datagv.Columns[AssemblySeqCol.Index].ReadOnly = true;
            datagv.Columns[OprSeqCol.Index].ReadOnly = true;
            button4.Enabled = false;
        }

        private void SaveData()
        {
            string SQLStr = string.Empty;
            for (int i = 0; i < datagv.Rows.Count; i++)
            {
                if ((Boolean)datagv[IsChangedCol.Index, i].FormattedValue )
                {
                    SQLStr += "UPDATE JobOper SET CheckBox03='" + datagv[ShowOnKanbanCol.Index, i].FormattedValue.ToString() + "',ShortChar05='" + datagv[LIZENZCol.Index, i].Value.ToString() + "',ShortChar06='" + datagv[STANDORTCol.Index, i].Value.ToString() + "' WHERE Company = 'NSZ' AND JobNum='" + datagv[JobNumCol.Index, i].Value.ToString() + "' AND AssemblySeq=" + datagv[AssemblySeqCol.Index, i].Value.ToString() + " AND OprSeq=" + datagv[OprSeqCol.Index, i].Value.ToString() + ";" + Environment.NewLine;
                    SQLStr += "UPDATE JobHead SET CheckBox05='" + datagv[stopCol.Index, i].FormattedValue.ToString() + "' WHERE Company = 'NSZ' AND JobNum='" + datagv[JobNumCol.Index, i].Value.ToString() + "'" + Environment.NewLine;
                }
            }

            if (SQLStr != string.Empty)
            {
                DBHelper.ExecuteSql(SQLStr);
                //MessageBox.Show(SQLStr);
            }

            button4.Enabled = false;
        }
    }
}